﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using XGameFrame.Graphic;

namespace XGameFrame.Controls
{
    public delegate void OnCheckedChange(Boolean e);
    public class Checkbox:Control
    {
        #region Event
        public event OnCheckedChange onCheckedChangeEvent;
        #endregion


        #region Fields Region

        static bool isChecked;
        Texture2D nonChecked;
        Texture2D hoverCheck;
        Texture2D hovernonChecked;
        Texture2D check;
        #endregion

        #region Property Region
        /// <summary>
        /// the option is checked or unchecked
        /// true = checked
        /// false = unchecked
        /// </summary>
        public static bool IsChecked
        {
            get { return isChecked; }
            set { isChecked = value; }

        }

        /// <summary>
        /// the picture if the checkbox is nonchecked
        /// </summary>
        public Texture2D NonChecked
        {
            get { return nonChecked; }
            set { nonChecked = value; }
        }

        /// <summary>
        /// The Hover Picture
        /// </summary>
        public Texture2D Hover
        {
            get { return hoverCheck; }
            set { hoverCheck = value; }
        }
        /// <summary>
        /// The Checked Picture
        /// </summary>
        public Texture2D Checked
        {
            get { return check; }
            set { check = value; }
        }
         /// <summary>
         /// The hovered noncheck symbol
         /// </summary>
        public Texture2D HoverNonChecked
        {
            get { return hovernonChecked; }
            set { hovernonChecked = value; }
        }
        #endregion

        #region Constructor

        public Checkbox(Texture2D pNonChecked,Texture2D pCheck ,Rectangle pDestination)
            :base(pNonChecked,new Vector2(pDestination.X,pDestination.Y))
        {
            NonChecked = pNonChecked;
            HoverNonChecked = pNonChecked;
            Checked = pCheck;
            Hover = pCheck;
            SourceRectangle = new Rectangle(0, 0, pNonChecked.Width, pNonChecked.Height);
            DestRectangle = pDestination;
            Bounds = new Rectangle(0, 0, Checked.Width, Checked.Height);
            Color = Color.White;
        }

        public Checkbox(Texture2D pNonChecked, Texture2D pCheck, Texture2D pHover, Rectangle pDestination)
            : base(pNonChecked, new Vector2(pDestination.X, pDestination.Y))
        {
            NonChecked = pNonChecked;
            Checked = pCheck;
            HoverNonChecked = pNonChecked;
            Hover = pHover;
            SourceRectangle = new Rectangle(0, 0, pNonChecked.Width, pNonChecked.Height);
            DestRectangle = pDestination;
            Bounds = new Rectangle(0, 0, Checked.Width, Checked.Height);
            Color = Color.White;
        }

        public Checkbox(Texture2D pNonChecked, Texture2D pCheck,Texture2D pHoverNonChecked, Texture2D pHover, Rectangle pDestination)
            : base(pNonChecked, new Vector2(pDestination.X, pDestination.Y))
        {
            NonChecked = pNonChecked;
            Checked = pCheck;
            HoverNonChecked = pHoverNonChecked;
            Hover = pHover;
            SourceRectangle = new Rectangle(0, 0, pNonChecked.Width, pNonChecked.Height);
            DestRectangle = pDestination;
            Bounds = new Rectangle(0, 0, Checked.Width, Checked.Height);
            Color = Color.White;
        }

        #endregion

        #region Abstract Method Region

        public override void Update(GameTime gameTime)
        {
            if (IsChecked)
            {
                if (IsHover)
                {
                    SourceRectangle = new Rectangle(0, 0, Hover.Width, Hover.Height);
                }
                else
                {
                    SourceRectangle = new Rectangle(0, 0, Checked.Width, Checked.Height);
                }
            }
            else
            {
                if (IsHover)
                {
                    SourceRectangle = new Rectangle(0, 0, HoverNonChecked.Width, HoverNonChecked.Height);
                }
                else
                {
                    SourceRectangle = new Rectangle(0, 0,NonChecked.Width, NonChecked.Height);
                }
            }
            Bounds = new Rectangle((int)Position.X, (int)Position.Y, DestRectangle.Width, DestRectangle.Height);
            DestRectangle = new Rectangle((int)Position.X, (int)Position.Y, SourceRectangle.Width, SourceRectangle.Height);
            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (IsChecked)
            {
                if (IsHover)
                {
                    spriteBatch.Draw(Hover, Position, SourceRectangle, Color.Aqua, Rotation, Origin, Scale, SpriteEffect, LayerDepth);
                }
                else
                {
                    spriteBatch.Draw(Checked, Position, SourceRectangle, Color, Rotation, Origin,Scale, SpriteEffect, LayerDepth);
                }
            }
            else
            {
                if (IsHover)
                {
                    spriteBatch.Draw(HoverNonChecked, Position, SourceRectangle, Color.Pink, Rotation, Origin, Scale, SpriteEffect, LayerDepth);
                }
                else
                {
                    spriteBatch.Draw(NonChecked, Position, SourceRectangle, Color, Rotation, Origin,Scale, SpriteEffect, LayerDepth);
                }
            }

        }

        public override void HandleInput(PlayerIndex playerIndex)
        {
            
        }

        public override void OnHover(PlayerIndex playerIndex)
        {
            if (InputHandler.MouseButtonLeftPressed())
                ChangeIsChecked();
        }

        #endregion

        #region Checkbox Methods

        public virtual void ChangeIsChecked()
        {
            IsChecked = !IsChecked;
            throwOnCheckedChange(IsChecked);
        }

        private void throwOnCheckedChange(Boolean e)
        {
            if (onCheckedChangeEvent != null)
                onCheckedChangeEvent(e);
        }

        #endregion

    }
}
